package com.zd.wechatPlatform.quartz;

import java.util.Date;
import java.util.List;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.zd.wechatPlatform.bean.model.WxAccount;
import com.zd.wechatPlatform.cache.map.WxAccountCache;
import com.zd.wechatPlatform.common.utils.LogUtils;
import com.zd.wechatPlatform.common.utils.WeixinInterfaceUtil;
import com.zd.wechatPlatform.service.IWxAccountService;

public class RefreshTokenTimer {
	private WxAccountCache wxAccountCache;

	private IWxAccountService wxAccountService;

	public void refreshToken() {
		try {
			//更新缓存中的公众号信息
			wxAccountCache.loadWxAccounts();

			List<WxAccount> wxAccountList = wxAccountCache.getAllWxAccount();
			if (wxAccountList != null && wxAccountList.size() > 0) {
				//刷新每个公众号的access_token 和jsapiTicket
				for (WxAccount wxAccount : wxAccountList) {
					if(wxAccount.getIsRefreshToken() == 0) {
						//如果不刷新 直接跳出去
						continue;
					}
					
					String accessToken = null;
					String jsapiTicket = null;
					//循环获取  获取成功为止
					while (accessToken == null) {
						String accessTokenRespone = WeixinInterfaceUtil.genAccessToken(wxAccount.getAppId(),
								wxAccount.getAppSecret());
						LogUtils.info("appId : " + wxAccount.getAppId() + ",appSecret : " + wxAccount.getAppSecret());
						LogUtils.debug("getAccessToken.message:" + accessTokenRespone);
						JSONObject json = JSONObject.parseObject(accessTokenRespone);
						String errcode = json.getString("errcode");
						String errmsg = json.getString("errmsg");
						if (StringUtils.isEmpty(errcode) || "0".equals(errcode)) {
							accessToken = json.getString("access_token");
						} else {
							LogUtils.error("genAccessToken.errcode:" + errcode + ",errmsg:" + errmsg + "appid:");
						}

						if (accessToken != null) {
							while (jsapiTicket == null) {
								String jsapiTicketRespone = WeixinInterfaceUtil.genJsapiTikcet(accessToken);
								LogUtils.debug("genJsapiTikcet.message:" + jsapiTicketRespone);
								JSONObject ticketJson = JSONObject.parseObject(jsapiTicketRespone);
								String tickErrcode = ticketJson.getString("errcode");
								String tickErrMsg = ticketJson.getString("errmsg");
								if (StringUtils.isEmpty(tickErrcode) || "0".equals(tickErrcode)) {
									jsapiTicket = ticketJson.getString("ticket");
								} else {
									LogUtils.error("genJsapiTikcet.errcode:" + tickErrcode + ",errmsg:" + tickErrMsg);
								}
							}
						}
					}
					if (accessToken != null) {
						wxAccount.setAccessToken(accessToken);
					}

					if (jsapiTicket != null) {
						wxAccount.setJsapiTicket(jsapiTicket);
					}
					wxAccount.setUpdateTime(new Date());
					wxAccountService.update(wxAccount);
				}
			}
			
			//刷新缓存中公众号信息
			wxAccountCache.loadWxAccountMap();
			wxAccountCache.loadWxAccountAppMap();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public WxAccountCache getWxAccountCache() {
		return wxAccountCache;
	}

	public void setWxAccountCache(WxAccountCache wxAccountCache) {
		this.wxAccountCache = wxAccountCache;
	}

	public IWxAccountService getWxAccountService() {
		return wxAccountService;
	}

	public void setWxAccountService(IWxAccountService wxAccountService) {
		this.wxAccountService = wxAccountService;
	}
}
